//
//  ContentViewController.m
//  ExhibitForcePrototype
//
//  Created by Georgi Bachvarov on 12/5/12.
//  Copyright (c) 2012 Dilyana Todorova. All rights reserved.
//

#import "ContentViewController.h"
#import "TextDocumentViewController.h"
#import "PlayVideoViewController.h"
#import "QuestionnaireViewController.h"
#import "CheckoutViewController.h"


@interface ContentViewController ()<QuestinairreDelegate>

@property (nonatomic, strong)  NSMutableArray *categories;
@property (nonatomic, strong) NSDictionary *items;

@property (nonatomic, strong) NSString *selectedCategory;

@end

@implementation ContentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
         self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Content" image:[UIImage imageNamed:@"46-movie-2"] tag:0];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.contentTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    self.categories = [[NSMutableArray alloc] initWithObjects:@"Electronics", @"Work", @"Vacation", nil];
    self.items = @{ @"Electronics": [NSArray arrayWithObjects:@"MSI GE60 demo video",@"Soni Bravia Commercial", nil], @"Work":[NSArray arrayWithObjects:@"New Office", @"Management Retreat", @"Instructional Fire Emergengy Video" ,nil], @"Vacation": [NSArray arrayWithObjects: @"Vacation Slides", @"Summer 2011", @"Family Trip", @"Christmas Play",nil] };
    
}

#pragma mark Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0){
        return self.categories.count;
    }else{
        return [[self.items objectForKey:self.selectedCategory] count];
    }
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSString * ) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return @"Category";
    }else{
        return @"Content";
    }
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    if (indexPath.section == 0){
        cell.textLabel.text = [self.categories objectAtIndex:indexPath.row];
    }else{
        cell.textLabel.text = [[self.items objectForKey:self.selectedCategory] objectAtIndex:indexPath.row];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        self.selectedCategory = [self.categories objectAtIndex:indexPath.row];
        [self.contentTableView reloadData];
    }else{
        
    }
}
#pragma mark - actions
- (IBAction)pdfButtonPressed:(id)sender {
    TextDocumentViewController *tdvt = [[TextDocumentViewController alloc] initWithNibName:@"TextDocumentViewController" bundle:nil];
    tdvt.delegate = self;
    //[[self.tabBarController navigationController] pushViewController:tdvt animated:YES];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tdvt];
    navController.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:navController animated:YES completion:nil];
}
- (IBAction)videoButtonPressed:(id)sender {
    PlayVideoViewController *pvvc = [[PlayVideoViewController alloc] init];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:pvvc];
    nc.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:nc animated:YES completion:nil];
}
- (IBAction)checkoutButtonPressed:(id)sender {
    QuestionnaireViewController *qvc = [[QuestionnaireViewController alloc] initWithNibName:@"QuestionnaireViewController" bundle:nil];
    qvc.questionnaireDelegate = self;
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:qvc];
    nc.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:nc animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - implement MMModalViewController methods
- (void) didDismissDetailView{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark- questionnaire delegate
-(void) questionnaireFinished
{
    CheckoutViewController *cvc = [[CheckoutViewController alloc] initWithNibName:@"CheckoutViewController" bundle:nil];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:cvc];
    nc.modalPresentationStyle = UIModalPresentationPageSheet;
    [self presentViewController:nc animated:YES completion:nil];
}
@end
